package com.eng.carro.mb;

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.eng.carro.entities.Order;
import com.eng.carro.services.OrderService;
import com.eng.carro.services.UserInfo;

@ManagedBean( name = "orderMB" )
@SessionScoped
public class OrderMBean implements Serializable {
  
  private static final long serialVersionUID = -1655395250866569520L;

  public Order order;
  
  @EJB OrderService service;
  
  private List<Order> values;
  
  public List<Order> getValues() {
	if ( values == null ) {
	  values = service.ordersByPriorityByUser();
	}
	
    return values;
  }

  public void atualiza() {
	setValues(null);
  }
  
  public void enviarAPqp( Order p ) {
	try {
	  p.setStatus(4);
	  service.makeUpdate(p);
	  values = null;
	} catch (Exception e) {
	  e.printStackTrace();
	}
  }
  
  public void setValues(List<Order> values) {
    this.values = values;
  }

  @PostConstruct
  public void init() {
	order = new Order();
  }

  public Order getOrder() {
    return order;
  }

  public void setOrder(Order order) {
    this.order = order;
  }
  
  public String getPrioridadeTexto(Integer i) {
 	return Order.PRIORITY[i];
  }
  
  public void inserir() {
	order.setDate( Calendar.getInstance() );
	order.setStatus( 0 );
	order.setDeliveryPlace( UserInfo.getInstance().getCurrentUser().getDefaultPlace() );
	
	service.makePersistent(order);
	
	setOrder(new Order());
	values = null;
  }
  
}
